package de.clue.corpus.twitter.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import de.clue.corpus.twitter.bean.DataObject;
import de.clue.corpus.twitter.bean.FileLister;
import de.clue.corpus.twitter.bean.TweetLister;
import de.clue.corpus.twitter.bean.XMLFileLister;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.widgets.Label;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.swt.custom.StyledText;
import java.io.File;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import noNamespace.TweetDocument.Tweet;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.DoubleClickEvent;

public class TwitterMessageGUI extends Shell {
	private DataBindingContext m_bindingContext;
	private Table tblDataObjects;
	private Text txtDirectory;
	
	private FileLister fileLister = new FileLister();
	private XMLFileLister xmlFileLister = new XMLFileLister();
	private TweetLister tweetLister = new TweetLister();
	private TableViewer tableViewer;
	private Text txtName;
	private Text txtText;
	private Text txtPath;
	private Table tblTweets;
	private TableViewer tblViewTweets;
	private Text txtPolarity;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					Display display = Display.getDefault();
					TwitterMessageGUI shell = new TwitterMessageGUI(display);
					shell.open();
					shell.layout();
					while (!shell.isDisposed()) {
						if (!display.readAndDispatch()) {
							display.sleep();
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public TwitterMessageGUI(Display display) {
		super(display, SWT.SHELL_TRIM);
		
		SashForm sashForm_1 = new SashForm(this, SWT.NONE);
		sashForm_1.setSize(625, 544);
		sashForm_1.setOrientation(SWT.VERTICAL);
		sashForm_1.setLocation(10, 10);
		
		SashForm sashForm = new SashForm(sashForm_1, SWT.NONE);
		sashForm.setSashWidth(5);
		
		Group grpMessages = new Group(sashForm, SWT.NONE);
		grpMessages.setText("Files");
		
		tableViewer = new TableViewer(grpMessages, SWT.BORDER | SWT.FULL_SELECTION);
		tableViewer.addDoubleClickListener(new IDoubleClickListener() {
		    public void doubleClick(DoubleClickEvent arg0) {
		        tblViewTweets.refresh();
		    }
		});
		tblDataObjects = tableViewer.getTable();
		tblDataObjects.setBounds(10, 85, 287, 175);
		
		txtDirectory = new Text(grpMessages, SWT.BORDER);
		txtDirectory.setText("D:\\home\\andre\\twitter\\workspace_twitter\\TwitterCorpus\\output\\");
		txtDirectory.setBounds(10, 27, 287, 21);
		
		Button btnNewButton = new Button(grpMessages, SWT.NONE);
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				String parentDirectory = txtDirectory.getText();
				if(parentDirectory != null){
					//fileLister.setParentDirectory(parentDirectory);
				    xmlFileLister.setHomeDirectory(parentDirectory);
					tableViewer.refresh();
					
				}
				
			}
		});
		btnNewButton.setBounds(10, 54, 75, 25);
		btnNewButton.setText("Read");
		
		Group grpTweets = new Group(sashForm, SWT.NONE);
		grpTweets.setText("Tweets");
		
		tblViewTweets = new TableViewer(grpTweets, SWT.BORDER | SWT.FULL_SELECTION);
		tblTweets = tblViewTweets.getTable();
		tblTweets.setBounds(10, 26, 287, 234);
		sashForm.setWeights(new int[] {313, 307});
		
		Group grpMessage = new Group(sashForm_1, SWT.NONE);
		grpMessage.setText("Message");
		
		Label lblName = new Label(grpMessage, SWT.NONE);
		lblName.setBounds(10, 27, 55, 15);
		lblName.setText("Name");
		
		txtName = new Text(grpMessage, SWT.BORDER);
		txtName.setEditable(false);
		txtName.setBounds(71, 24, 129, 21);
		
		Label lblText = new Label(grpMessage, SWT.NONE);
		lblText.setBounds(10, 60, 55, 15);
		lblText.setText("Text");
		
		txtText = new Text(grpMessage, SWT.BORDER | SWT.WRAP);
		txtText.setBounds(10, 81, 401, 158);
		
		txtPath = new Text(grpMessage, SWT.BORDER);
		txtPath.setEditable(false);
		txtPath.setBounds(10, 245, 575, 21);
		
		Button btnNegativ = new Button(grpMessage, SWT.NONE);
		btnNegativ.setBounds(427, 81, 137, 25);
		btnNegativ.setText("Negativ (&N)");
		
		Button btnNeutral = new Button(grpMessage, SWT.NONE);
		btnNeutral.setText("Neutral (&M)");
		btnNeutral.setBounds(427, 143, 137, 25);
		
		Button btnPositiv = new Button(grpMessage, SWT.NONE);
		btnPositiv.setText("Positiv (&B)");
		btnPositiv.setBounds(427, 112, 137, 25);
		
		Label lblPolarity = new Label(grpMessage, SWT.NONE);
		lblPolarity.setBounds(217, 27, 67, 15);
		lblPolarity.setText("Polarity");
		
		txtPolarity = new Text(grpMessage, SWT.BORDER);
		txtPolarity.setEditable(false);
		txtPolarity.setBounds(290, 24, 121, 21);
		sashForm_1.setWeights(new int[] {1, 1});
		createContents();
		m_bindingContext = initDataBindings();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("TM Display");
		setSize(661, 600);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
    protected DataBindingContext initDataBindings() {
        DataBindingContext bindingContext = new DataBindingContext();
        //
        ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
        tableViewer.setContentProvider(listContentProvider);
        //
        IObservableMap observeMap = PojoObservables.observeMap(listContentProvider.getKnownElements(), File.class, "name");
        tableViewer.setLabelProvider(new ObservableMapLabelProvider(observeMap));
        //
        IObservableList xmlFileListerFilesInDirectoryObserveList = PojoObservables.observeList(Realm.getDefault(), xmlFileLister, "filesInDirectory");
        tableViewer.setInput(xmlFileListerFilesInDirectoryObserveList);
        //
        IObservableValue tableViewerObserveSingleSelection = ViewersObservables.observeSingleSelection(tableViewer);
        IObservableValue tableViewerAbsoluteFileObserveDetailValue = PojoObservables.observeDetailValue(tableViewerObserveSingleSelection, "absoluteFile", File.class);
        IObservableValue tweetListerCurrentXMLObserveValue = PojoObservables.observeValue(tweetLister, "currentXML");
        bindingContext.bindValue(tableViewerAbsoluteFileObserveDetailValue, tweetListerCurrentXMLObserveValue, null, null);
        //
        ObservableListContentProvider listContentProvider_1 = new ObservableListContentProvider();
        tblViewTweets.setContentProvider(listContentProvider_1);
        //
        IObservableMap observeMap_1 = PojoObservables.observeMap(listContentProvider_1.getKnownElements(), Tweet.class, "author");
        tblViewTweets.setLabelProvider(new ObservableMapLabelProvider(observeMap_1));
        //
        IObservableList tweetListerTweetsObserveList = PojoObservables.observeList(Realm.getDefault(), tweetLister, "tweets");
        tblViewTweets.setInput(tweetListerTweetsObserveList);
        //
        IObservableValue tblViewTweetsObserveSingleSelection = ViewersObservables.observeSingleSelection(tblViewTweets);
        IObservableValue tblViewTweetsAuthorObserveDetailValue = PojoObservables.observeDetailValue(tblViewTweetsObserveSingleSelection, "author", String.class);
        IObservableValue txtNameTextObserveValue = PojoObservables.observeValue(txtName, "text");
        bindingContext.bindValue(tblViewTweetsAuthorObserveDetailValue, txtNameTextObserveValue, null, null);
        //
        IObservableValue tblViewTweetsObserveSingleSelection_1 = ViewersObservables.observeSingleSelection(tblViewTweets);
        IObservableValue tblViewTweetsTextObserveDetailValue = PojoObservables.observeDetailValue(tblViewTweetsObserveSingleSelection_1, "text", String.class);
        IObservableValue txtTextTextObserveValue = PojoObservables.observeValue(txtText, "text");
        bindingContext.bindValue(tblViewTweetsTextObserveDetailValue, txtTextTextObserveValue, null, null);
        //
        IObservableValue tblViewTweetsObserveSingleSelection_2 = ViewersObservables.observeSingleSelection(tblViewTweets);
        IObservableValue tblViewTweetsPolarityObserveDetailValue = PojoObservables.observeDetailValue(tblViewTweetsObserveSingleSelection_2, "polarity", String.class);
        IObservableValue txtPolarityTextObserveValue = PojoObservables.observeValue(txtPolarity, "text");
        bindingContext.bindValue(tblViewTweetsPolarityObserveDetailValue, txtPolarityTextObserveValue, null, null);
        //
        return bindingContext;
    }
}
